package com.example.demo13;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private MySQLite mySQLite;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mySQLite = new MySQLite(getApplicationContext(),"BookStore",null, 1);
        Button createDB = findViewById(R.id.create_bookStore);
        Button readBook = findViewById(R.id.read_table_book);
        Button insertBook = findViewById(R.id.insert_table_book);
        Button deleteBook = findViewById(R.id.delete_table_book);
        Button updateBook = findViewById(R.id.update_table_book);

        final TextView text=findViewById(R.id.text);
        final EditText authorView=findViewById(R.id.author);
        final EditText priceView=findViewById(R.id.price);
        final EditText pagesView=findViewById(R.id.pages);
        final EditText bookNameView=findViewById(R.id.book_name);
        //创建数据库
        final SQLiteDatabase readableDatabase = mySQLite.getReadableDatabase();
        createDB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        insertBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase database = mySQLite.getWritableDatabase();
                ContentValues contentValues=new ContentValues();
                //组装数据
                contentValues.put("author",authorView.getText().toString());
                contentValues.put("price",priceView.getText().toString());
                contentValues.put("pages",pagesView.getText().toString());
                contentValues.put("name",bookNameView.getText().toString());
                //插入数据
                long book = database.insert("book",null, contentValues);
                //清除数据
                contentValues.clear();
            }
        });
        updateBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //更新数据
                SQLiteDatabase database = mySQLite.getWritableDatabase();
                ContentValues contentValues = new ContentValues();
                contentValues.put("author",authorView.getText().toString());
                contentValues.put("price",priceView.getText().toString());
                contentValues.put("pages",pagesView.getText().toString());
                contentValues.put("name",bookNameView.getText().toString());
                database.update("book",contentValues,"name = ?",new String[]{
                        bookNameView.getText().toString()
                });
                contentValues.clear();
            }
        });
        deleteBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase database = mySQLite.getWritableDatabase();
                //删除数据
                database.delete("book","name = ?",new String[]{bookNameView.getText().toString()});

            }
        });
        readBook.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor book = readableDatabase.query("book", null, null, null, null, null, null, null);
                StringBuilder rows=new StringBuilder();
                if (book.moveToFirst()){
                    do {
                        rows.append("书名:"+book.getString(4)+"作者:"+book.getString(1)+"" +
                                "单价:"+book.getFloat(2)+"" +
                                "页数:"+book.getInt(3)+"\n");
                    }while (book.moveToNext());
                }
                String data = null;
                data=rows.toString();
                text.setText(data);
            }
        });
    }


    //定义数据库工具类
    class MySQLite extends SQLiteOpenHelper{
        final String CREATE_BOOK = "create table book(" +
                "id integer primary key autoincrement," +
                "author text," +
                "price real," +
                "pages integer," +
                "name text)";
        private Context mContext;
        public MySQLite(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
            mContext = context;
        }

        public MySQLite(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version, @Nullable DatabaseErrorHandler errorHandler) {
            super(context, name, factory, version, errorHandler);
            mContext = context;

        }

        @RequiresApi(api = Build.VERSION_CODES.P)
        public MySQLite(@Nullable Context context, @Nullable String name, int version, @NonNull SQLiteDatabase.OpenParams openParams) {
            super(context, name, version, openParams);
            mContext = context;
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            //创建表
            db.execSQL(CREATE_BOOK);
            Toast.makeText(mContext,"数据库创建成功",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
    }
}
